package info.niwota.home.fm;

import info.niwota.home.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import x.java.io.File;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author qiangli
 * 
 */
public class FileListAdapter extends ArrayAdapter<File> {
	private static class ViewHolder {
		TextView desc;
		ImageView icon;
		StringBuffer sb;
		TextView title;
	}

	private static DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");

	private static DateFormat df2 = new SimpleDateFormat("hh:mm:ss");
	
	private static final long MILLS_PER_DAY = 24 * 60 * 60 * 1000;

	private static void formatDate(Date date, StringBuffer sb) {
		sb.append(df1.format(date));
	}

	private static void formatHour(Date date, StringBuffer sb) {
		sb.append(df2.format(date));
	}

	public static void formatSize(long size, StringBuffer sb) {
		if (size < 0) {
			sb.append("-");
			return;
		}
		if (size < 1024) {
			sb.append(size);
			sb.append("B");
			return;
		}
		size /= 1024;
		if (size < 1024) {
			sb.append(size);
			sb.append("KB");
			return;
		}
		size /= 1024;
		sb.append(size);
		sb.append("MB");
		return;
	}

	private LayoutInflater inflater;

	public FileListAdapter(Context ctx, List<File> items) {
		super(ctx, 0, items);

		inflater = LayoutInflater.from(ctx);

	}

	private static void format(File item, StringBuffer sb) {
		long mod = item.lastModified();
		if (mod > 0) {
			Date da = new Date(mod);
			long now = System.currentTimeMillis();
			boolean old = (mod < now - MILLS_PER_DAY);
			if (old) {
				formatDate(da, sb);
			} else {
				formatHour(da, sb);
			}
			sb.append(" ");
		}
		long len = item.length();
		if (len > 0) {
			formatSize(len, sb);
		}
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.filelist_item2, parent,
					false);
			holder = new ViewHolder();
			holder.title = (TextView) convertView
					.findViewById(R.id.filelist_item2_title);
			holder.desc = (TextView) convertView
					.findViewById(R.id.filelist_item2_desc);
			holder.icon = (ImageView) convertView
					.findViewById(R.id.filelist_item2_icon);
			holder.sb = new StringBuffer();
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		//
		final File item = getItem(position);
		String ti = null;
		String desc = null;
		boolean dir = item.isDirectory();
		if (dir) {
			String n = item.getDisplayName();
			ti = ("..".equals(n) ? n : n + "/");
			IconUtils.setFolderIcon(holder.icon, item);
			desc = "";
		} else {
			ti = item.getDisplayName();
			IconUtils.setFileIcon(holder.icon, item);
			//
			StringBuffer sb = holder.sb;
			sb.setLength(0);
			format(item, sb);
			desc = sb.toString();
		}

		holder.title.setText(ti);
		holder.desc.setText(desc);
		//
		return convertView;
	}
}
